package com.personnalManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.personnalManagerSystem.model.Permission;
import com.personnalManagerSystem.model.RolePermission;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/role-permission")
public class RolePermissionController  extends BaseController{
    @RequestMapping("FPpermission")
    @ResponseBody
    /**
     * @Description  分配权限用于拥有权限数据回显
     * @author liqing
     * @data 2022/3/30  15:48
     * @param roleId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> edit(Integer roleId){
        //System.out.println("roleId:"+roleId);
        Map<String,Object> map = new HashMap<>();
        //在role-permission表中根据roleId查询出相关数据
        List<RolePermission> rolePermissionList = iRolePermissionService.list(new QueryWrapper<RolePermission>().eq("role_id", roleId));
        //验证该roleId在该关系表中是否有记录,若无,则返回对应的信息
        if(rolePermissionList.size()<1){
            map.put("state","isNull");
            map.put("permissionNameList","");
            return map;
        }
        //取出角色权限中的权限id表
        List<Integer> permissionIdList = rolePermissionList.stream()
                .map(RolePermission::getPermissionId)
                .collect(Collectors.toList());
        //根据权限id表查出对应的权限表
        List<Permission> permissionList = iPermissionService.list(new QueryWrapper<Permission>().in("permission_id", permissionIdList));
        //取出权限表中对应的permissionName表
        List<String> permissionNameList = permissionList.stream()
                .map(Permission::getPermissionName)
                .collect(Collectors.toList());
        //System.out.println("permissionNameList:"+permissionNameList);
        //存入查出的permissionName
        map.put("state","ok");
        map.put("permissionNameList",permissionNameList);
        return map;
    }
    /**
     * @Description  分配权限,在为角色分配权限之前要先清除角色权限表中与传入角色Id的所有记录
     * @author liqing
     * @data 2022/3/30  15:48
     * @param roleId
     * @param permissionIds
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @RequestMapping("/fpCommit")
    @ResponseBody
    public Map<String,Object> fpCommit(@RequestParam("roleId")Integer roleId,
                                       @RequestParam("permissionIds")List<String> permissionIds ){
        System.out.println("roleId:"+roleId);
        System.out.println("permissionIds:"+permissionIds);
        Map<String,Object> map = new HashMap<>();

        //清空角色权限关系表中与传入角色Id的所有记录
        iRolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id",roleId));
        //将角色Id和权限id转换为角色权限关系对象并存入
        ArrayList<RolePermission> rolePermissionList = new ArrayList<>();
        for(String permissionId:permissionIds){
            RolePermission rolePermission = new RolePermission();
            //设置角色Id
            rolePermission.setRoleId(roleId);
            //设置权限id
            rolePermission.setPermissionId(Integer.parseInt(permissionId));
            //加入rolePermission集合中
            rolePermissionList.add(rolePermission);
        }
        //将List存入数据库,就完成了批量存入数据
        iRolePermissionService.saveOrUpdateBatch(rolePermissionList);
        map.put("code",200);
        return map;
    }
}

